﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using H2.NET.Controls;
using H2.NET.Data.Common;
using H2.NET.Business;

namespace H2.NET.Web.Pages
{
    public partial class TabBarcode : TaskControl
    {
        #region 属性 Public
        public MDCTabStatus Status
        {
            get
            {
                if (StoreAPI[Key + "~" + ID] == null)
                {
                    StoreAPI[Key + "~" + ID] = new MDCTabStatus();
                }

                return (MDCTabStatus)StoreAPI[Key + "~" + ID];
            }
            set
            {
                Key = value.KeyWord;
                StoreAPI[Key + "~" + ID] = value;
            }
        }

        public string Key
        {
            get
            {
                return ViewState["Key"] == null ? "" : (string)ViewState["Key"];

            }
            set { ViewState["Key"] = value; }
        }

        public int SheetIndex
        {
            get
            {
                return ViewState["SheetIndex"] == null ? 0 : (int)ViewState["SheetIndex"];

            }
            set { ViewState["SheetIndex"] = value; }
        }
        #endregion

        #region 事件 Load(页面)
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            //如果当前 Tabs 是隐藏的情况，退出
            if (!Status.IsUsed)
            {
                return;
            }

            bool flg = Status.QA == "C" && TaskAPI.IsAllowSubmit;
            gv_Barcode.DynamicColumn.Add(new BoundColumn2("00", "No", BoundColumnType2.TextBox, 20, Align.Left, flg, "No", InputType.None, null));
            gv_Barcode.DynamicColumn.Add(new BoundColumn2("01", "工厂", BoundColumnType2.TextBox, 50, Align.Left, flg, "Plant", InputType.None, null));
            gv_Barcode.DynamicColumn.Add(new BoundColumn2("02", "产品编号", BoundColumnType2.TextBox, -1, Align.Left, flg, "SKU_Code", InputType.None, null));
            gv_Barcode.DynamicColumn.Add(new BoundColumn2("03", "SAP CODE", BoundColumnType2.TextBox, -1, Align.Left, flg, "SAP_Code", InputType.None, null));
            gv_Barcode.DynamicColumn.Add(new BoundColumn2("04", "中文名称", BoundColumnType2.TextBox, -1, Align.Left, flg, "Prd_Grp", InputType.None, null));
            gv_Barcode.DynamicColumn.Add(new BoundColumn2("05", "包装<br/>容量", BoundColumnType2.TextBox, 40, Align.Left, flg, "Capacity", InputType.None, null));
            gv_Barcode.DynamicColumn.Add(new BoundColumn2("06", "单位", BoundColumnType2.TextBox, 30, Align.Left, flg, "Unit", InputType.None, null));
            gv_Barcode.DynamicColumn.Add(new BoundColumn2("07", "标签打印类型", BoundColumnType2.DropDownList, 80, Align.Left, flg, "Label_Type", InputType.None, new string[9] { "", "label", "Label_CX2", "label_CZ", "label_promotion", "label_SX", "label_tmp", "label_tmp", "Label_Levis" }));
            gv_Barcode.DynamicColumn.Add(new BoundColumn2("08", "标签", BoundColumnType2.DropDownList, 80, Align.Left, flg, "Label", InputType.None, new string[3] { "", "special", "normal" }));
            gv_Barcode.DynamicColumn.Add(new BoundColumn2("09", "企业标准", BoundColumnType2.TextBox, -1, Align.Left, flg, "Stadard", InputType.None, null));
            gv_Barcode.DynamicColumn.Add(new BoundColumn2("10", "符合标准", BoundColumnType2.TextBox, -1, Align.Left, flg, "StadardIs", InputType.None, null));
            gv_Barcode.DynamicColumn.Add(new BoundColumn2("11", "保质期", BoundColumnType2.TextBox, 40, Align.Left, flg, "Quarantee", InputType.None, null));
            gv_Barcode.DynamicColumn.Add(new BoundColumn2("12", "SKU", BoundColumnType2.TextBox, 64, Align.Left, flg, "SKU", InputType.None, null));
            gv_Barcode.DynamicColumn.Add(new BoundColumn2("13", "EAN码", BoundColumnType2.TextBox, 64, Align.Left, flg, "EAN", InputType.None, null));

            if (!Page.IsPostBack)
            {
                gv_Barcode.Key = Key;

                #region 发起步骤
                if (TaskAPI.IsInitiate)
                {
                    List<mdc_Barcode> blank = new List<mdc_Barcode>();
                    for (int i = 0; i < 10; i++)
                    {
                        blank.Add(new mdc_Barcode());
                        blank[i].No = i + 1;
                    }
                    gv_Barcode.DataSource = blank;
                }
                #endregion

                #region 审批环节
                else
                {
                    IList<mdc_Barcode> barcode = (mdc_Barcode.process == TaskAPI.Process & mdc_Barcode.incident == TaskAPI.Incident).List<mdc_Barcode>();
                    gv_Barcode.DataSource = barcode;
                }
                #endregion

                SetVisible(flg, Barcode_Add, Barcode_Delete);
            }
        }
        #endregion

        #region 事件 Add(增加行)
        protected void Add_Click(object sender, EventArgs e)
        {
            List<mdc_Barcode> barcode_s = (List<mdc_Barcode>)gv_Barcode.DataSource;
            mdc_Barcode barcode = new mdc_Barcode();
            barcode.No = barcode_s.Count + 1;
            barcode_s.Insert(gv_Barcode.ChangedRowIndex == -1 || gv_Barcode.ChangedRowIndex >= barcode_s.Count ? barcode_s.Count - 1 : gv_Barcode.ChangedRowIndex, barcode);

            for(int i=0; i<barcode_s.Count; i++)
            {
                barcode_s[i].No = i + 1; 
            }

            gv_Barcode.DataSource = barcode_s;
            gv_Barcode.DataBind();
        }
        #endregion

        #region 事件 Delete(删除行)
        protected void Delete_Click(object sender, EventArgs e)
        {
            List<mdc_Barcode> barcode_s = (List<mdc_Barcode>)gv_Barcode.DataSource;
            if (barcode_s.Count == 1)
            {
                return;
            }

            barcode_s.RemoveAt(gv_Barcode.ChangedRowIndex == -1 || gv_Barcode.ChangedRowIndex >= barcode_s.Count ? barcode_s.Count - 1 : gv_Barcode.ChangedRowIndex);
            for (int i = 0; i < barcode_s.Count; i++)
            {
                barcode_s[i].No = i + 1;
            }

            gv_Barcode.DataSource = barcode_s;
            gv_Barcode.DataBind();
        }
        #endregion

        #region 方法 Save(保存数据库)
        public void Save()
        {
            if (!Status.IsEdited)
            {
                return;
            }

            foreach (mdc_Barcode barcode in (List<mdc_Barcode>)gv_Barcode.DataSource)
            {
                if (string.IsNullOrEmpty(barcode.Plant))
                {
                    continue;
                }

                barcode.Process     = TaskAPI.Process;
                barcode.Incident    = TaskAPI.Incident;
                barcode.UserName    = UserAPI.UserName;
                barcode.Time        = System.DateTime.Now;
                barcode.Status      = 1;

                Storage.Save(barcode);
            }
        }
        #endregion
    }
}
